//
//  DownLoadViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/25.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "DownLoadViewController.h"
#import "DownLoadModel+CoreDataProperties.h"

@interface DownLoadViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)UITableView* tableView;

@end

@implementation DownLoadViewController

+(instancetype)sharedDownLoad{

    static DownLoadViewController* handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DownLoadViewController alloc] init];
    });
    return handle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"下载";
    [self.view addSubview:self.tableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuBtn"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self.downLoadCompleteArr removeAllObjects];
    [self.downLoadCompleteArr addObjectsFromArray:[[DataManager defauleDataManager] DataWithDownLoad]];
//    NSLog(@"%@",self.downLoadCompleteArr);
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)downLoadCompleteArr{

    if (!_downLoadCompleteArr) {
        _downLoadCompleteArr = [NSMutableArray array];
    }
    return _downLoadCompleteArr;
}

-(NSMutableArray *)downLoadListArr{

    if (!_downLoadListArr) {
        _downLoadListArr = [NSMutableArray array];
    }
    return _downLoadListArr;
}

-(UITableView *)tableView{

    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.downLoadListArr.count;
    }else {
        return self.downLoadCompleteArr.count;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CEll"];
    if (!cell) {
        cell  = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CEll"];
    }
    if (indexPath.section == 0) {
        DownLoadTool* downLoad = [self.downLoadListArr objectAtIndex:indexPath.row];
        //    cell.textLabel.text = [NSString stringWithFormat:@"0.00%%"];
        [downLoad didDownLoading:^(float progress) {
            cell.textLabel.text = [NSString stringWithFormat:@"%.2f%%",progress];
        } complete:^(NSString *filePath, PlayInfoModel* model) {
            cell.textLabel.text = @"已完成";
        }];
    }
    if (indexPath.section == 1) {
        DownLoadModel* model = [self.downLoadCompleteArr objectAtIndex:indexPath.row];
        cell.textLabel.text = model.title;
    }
       return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"正在下载";
    }else if(section == 1){
        return @"已下载";
    }
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
